package com.ruoyi.base.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import com.ruoyi.base.domain.BaseStudent;
import com.ruoyi.base.service.IBaseStudentService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生Controller
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Api("学生信息管理")
@RestController
@RequestMapping("/base/student")
public class BaseStudentController extends BaseController {
	@Autowired
	private IBaseStudentService baseStudentService;

	/**
	 * 查询学生列表
	 */
	@ApiOperation("获取学生列表")
	@PreAuthorize("@ss.hasPermi('base:student:list')")
	@GetMapping("/list")
	public TableDataInfo list(BaseStudent student) {
		startPage();
		List<BaseStudent> list = baseStudentService.selectBaseStudentList(student);
		return getDataTable(list);
	}

	/**
	 * 查询学生列表
	 */
	@ApiOperation("获取学生列表")
	@GetMapping("/select")
	public TableDataInfo select(BaseStudent student) {
		List<BaseStudent> list = baseStudentService.selectBaseStudentList(student);
		return getDataTable(list);
	}

	/**
	 * 导出学生列表
	 */
	@ApiOperation("导出学生列表")
	@PreAuthorize("@ss.hasPermi('base:student:export')")
	@Log(title = "学生", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BaseStudent student) {
		List<BaseStudent> list = baseStudentService.selectBaseStudentList(student);
		ExcelUtil<BaseStudent> util = new ExcelUtil<BaseStudent>(BaseStudent.class);
		return util.exportExcel(list, "学生");
	}

	/**
	 * 获取学生详细信息
	 */
	@ApiOperation("获取学生详细信息")
	@ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "int", paramType = "path")
	@PreAuthorize("@ss.hasPermi('base:student:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(baseStudentService.selectBaseStudentById(id));
	}

	/**
	 * 获取自己的学籍信息
	 */
	@ApiOperation("获取自己的学籍信息")
	@GetMapping(value = "/my")
	public AjaxResult myInfo() {
		return AjaxResult.success(baseStudentService.findStudentByUser(SecurityUtils.getLoginUser().getUser().getUserId()));
	}

	/**
	 * 获取学生详细信息
	 */
	@ApiOperation("获取学生详细信息")
	@ApiImplicitParam(name = "id", value = "学生用户ID", required = true, dataType = "int", paramType = "path")
	@GetMapping(value = "/user/{userId}")
	public AjaxResult getByUser(@PathVariable("userId") Long userId) {
		return AjaxResult.success(baseStudentService.findStudentByUser(userId));
	}

	/**
	 * 新增学生
	 */
	@ApiOperation("新增学生")
	@PreAuthorize("@ss.hasPermi('base:student:add')")
	@Log(title = "学生", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody BaseStudent student) {
		return toAjax(baseStudentService.insertStudent(student));
	}

	/**
	 * 修改学生
	 */
	@ApiOperation("修改学生")
	@PreAuthorize("@ss.hasPermi('base:student:edit')")
	@Log(title = "学生", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody BaseStudent student) {
		return toAjax(baseStudentService.updateStudent(student));
	}

	/**
	 * 删除学生
	 */
	@ApiOperation("删除学生")
	@ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "int", paramType = "path")
	@PreAuthorize("@ss.hasPermi('base:student:remove')")
	@Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(baseStudentService.deleteStudentByIds(ids));
	}

	/**
	 * 禁用学生
	 */
	@ApiOperation("禁用学生")
	@ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
	@PreAuthorize("@ss.hasPermi('base:student:edit')")
	@Log(title = "学生", businessType = BusinessType.UPDATE)
	@GetMapping("/disable/{ids}")
	public AjaxResult disable(@PathVariable Long[] ids) {
		return toAjax(baseStudentService.updateStudentStatus(ids, "0"));
	}

	/**
	 * 启用课程
	 */
	@ApiOperation("启用学生")
	@ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "int", paramType = "path")
	@PreAuthorize("@ss.hasPermi('base:student:edit')")
	@Log(title = "学生", businessType = BusinessType.UPDATE)
	@GetMapping("/enable/{ids}")
	public AjaxResult enable(@PathVariable Long[] ids) {
		return toAjax(baseStudentService.updateStudentStatus(ids, "1"));
	}

	@Log(title = "导入学生信息", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('base:student:import')")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
		ExcelUtil<BaseStudent> util = new ExcelUtil<BaseStudent>(BaseStudent.class);
		List<BaseStudent> studentList = util.importExcel(file.getInputStream());
		String operName = SecurityUtils.getUsername();
		String message = baseStudentService.importStudent(studentList, updateSupport, operName);
		return AjaxResult.success(message);
	}

	@GetMapping("/importTemplate")
	public AjaxResult importTemplate() {
		ExcelUtil<BaseStudent> util = new ExcelUtil<BaseStudent>(BaseStudent.class);
		return util.importTemplateExcel("学生数据");
	}
}
